package com.antdesignbackend.backend.controller;

import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.entity.City;
import com.antdesignbackend.backend.entity.Room;
import com.antdesignbackend.backend.entity.entityVo.CityVo;
import com.antdesignbackend.backend.entity.entityVo.RoomVo;
import com.antdesignbackend.backend.service.CityService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/city")
public class CityController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    ReturnVO result = new ReturnVO();
    private Integer menuId;
    private Integer userId;
    private Integer userType;
    private String roleID;
    @Autowired
    private CityService cityService;

    /**
     * 城市管理首页
     */
    @GetMapping("/index")
    @AuthToken
    public String index(String menuId, HttpServletRequest request) {
        this.menuId = Integer.parseInt(menuId);
        return "city/index";

    }
    /***
     * 前端分页查询
     *
     * @return
     * @throws SecurityException
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    @PostMapping("/tableSearch")
    @ResponseBody
    @AuthToken
    public ReturnVO tableSearch(@RequestBody CityVo data, HttpServletRequest request) throws NoSuchMethodException, SecurityException,
            IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        result = new ReturnVO();
        List<CityVo> datalist=new ArrayList<>();
        QueryWrapper<CityVo> wrapper =new QueryWrapper<>();
        //正常查询过滤条件
        if(data.getProvincecode()!=null && !data.getProvincecode().equals(""))
        {
            wrapper.like("provincecode",data.getProvincecode());
        }
        if(data.getCity()!=null && !data.getCity().equals(""))
        {
            wrapper.like("city",data.getCity());
        }
        if(data.getInitial()!=null && !data.getInitial().equals(""))
        {
            wrapper.like("initial",data.getInitial());
        }
        //先统计满足条件的数据总数，因为sql 不包括排序
        int total=cityService.count(wrapper);
        result.setTotal(total);
        //再查询
        if(total>0)
        {
            datalist=cityService.search(wrapper,new Page<>(data.getPageIndex(),data.getPageSize()));
        }
        result.setData(datalist);
        return result;
    }
    /***
     * 新增保存
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    @AuthToken
    public ReturnVO save(@RequestBody City city) throws Exception {

        result = new ReturnVO();
        boolean b = false;
        b=cityService.save(city);
        if(b==true){
            result.setMessage("保存成功！");
            result.setCode("200");
        }
        else
        {
            result.setMessage("保存失败！");
            result.setCode("500");
        }
        return result;
    }
    /***
     * 新增所有保存
     * @return
     */
    @PostMapping("/saveAll")
    @ResponseBody
    @AuthToken
    public ReturnVO saveAll(@RequestBody List<City> citys) throws Exception {
        result = new ReturnVO();
        boolean b = false;
        b=cityService.saveBatch(citys);
        if(b==true){
            result.setMessage("保存成功！");
            result.setCode("200");
        }
        else
        {
            result.setMessage("保存失败！");
            result.setCode("500");
        }
        return result;
    }
    /**
     * 根据Id删除
     */
    @DeleteMapping("/removeById/{id}")
    @ResponseBody
    @AuthToken
    public ReturnVO deleteById(@PathVariable("id") Integer id) throws Exception {
        result = new ReturnVO();
        boolean b = false;
        b=cityService.removeById(id);
        if(b==true){
            result.setMessage("删除成功！");
            result.setCode("200");
        }
        else
        {
            result.setMessage("删除失败！");
            result.setCode("500");
        }
        return result;
    }
    /**
     * 获得省级编码
     */
    @ResponseBody
    @GetMapping(value = "/getProvinceCode")
    @AuthToken
    public ReturnVO getProvinceCode(HttpServletRequest request) {
        result = new ReturnVO();
        QueryWrapper<City> wrapper =new QueryWrapper<City>();
        wrapper.eq("level",1);
        List<City> dataList = cityService.getBaseMapper().selectList(wrapper);
        result.setData(dataList);
        return result;
    }
    /**
     * 获得城市编码
     */
    @ResponseBody
    @GetMapping(value = "/getCityCode")
    @AuthToken
    public ReturnVO getCityCode(HttpServletRequest request) {
        result = new ReturnVO();
        String provincecode=request.getParameter("provincecode");
        QueryWrapper<City> wrapper =new QueryWrapper<City>();
        wrapper.eq("level",2);
        wrapper.eq("provincecode",provincecode);
        List<City> dataList = cityService.getBaseMapper().selectList(wrapper);
        result.setData(dataList);
        return result;
    }
    /**
     * 获得区县编码
     */
    @ResponseBody
    @GetMapping(value = "/getAreaCode")
    @AuthToken
    public ReturnVO getAreaCode(HttpServletRequest request) {
        result = new ReturnVO();
        String provincecode=request.getParameter("provincecode");
        QueryWrapper<City> wrapper =new QueryWrapper<City>();
        wrapper.eq("level",3);
        wrapper.eq("provincecode",provincecode);
        List<City> dataList = cityService.getBaseMapper().selectList(wrapper);
        result.setData(dataList);
        return result;
    }
}
